package com.aidex.admin.controller.system;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.query.QueryGenerator;
import com.aidex.system.domain.SysOss;
import com.aidex.system.service.SysOssService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Slf4j
@RestController
@RequestMapping("/system/oss")
public class SysOssController extends BaseController<SysOssService> {

    @PreAuthorize("@ss.hasPermi('system:oss:list')")
    @GetMapping("/page")
    public R page(SysOss entity,
                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                  HttpServletRequest req) {
        QueryWrapper<SysOss> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        Page<SysOss> page = new Page<>(pageNo, pageSize);
        IPage<SysOss> pageList = service.selectPage(page, queryWrapper);
        return R.data(pageList);
    }

    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile multipartFile) {
        try {
            service.upload(multipartFile);
            return R.success("上传成功！");
        } catch (Exception ex) {
            log.info(ex.getMessage(), ex);
            return R.fail("上传成功！");
        }
    }

    /**
     * 删除通对象存储文件
     */
    @Log(title = "对象存储", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ossIds}")
    public R remove(@PathVariable String[] ossIds) {
        return R.status(service.deleteBatchIds(Arrays.asList(ossIds)));
    }

}
